package com.android.hedgehog;

import java.util.ArrayList;

import android.graphics.Color;

import com.android.hedgehog.framework.Graphics;

/**
 * A new Message object is made each time, you want to display a new String
 *
 */
public class Message {
	public String message;
	public ArrayList<String> typedCharacters = new ArrayList<String>();
	public int tick;// count of total characters displayed
	public float tickTimer; // time needed for each tick
	float totalTime;
	float typeSpeed = .10f;
	int totalChrs = 27;// total characters per line
	int fontSize = 18;
	int font = 2;
	public boolean finishedTyping;
	Graphics graphics;

	public Message(Graphics graphics, String messageToDisplay) {
		message = messageToDisplay;
		this.graphics=graphics;
	}

	public void display(float deltaTime) {

		totalTime += deltaTime;

		if (totalTime > tickTimer) {
			tickTimer += typeSpeed;
			if (tick < message.length()) {
				tick++;
				buildOutput(message.substring(0, tick));
			} else {
				finishedTyping = true;
			}
		}
		for (int i = 0; i < typedCharacters.size(); i++) {
			graphics.drawUniversalText(typedCharacters.get(i), font, false, 5, 20 + (20 * i), fontSize, 255,
					Color.GREEN);
		}

	}

	private void buildOutput(String s) {
		typedCharacters.clear();
		int lastIndex = 0;
		for (int i = 0; i < (s.length() / totalChrs); i++) {
			lastIndex++;
			typedCharacters.add(s.substring(i * totalChrs, totalChrs * (i + 1)));
		}
		typedCharacters.add(s.substring(lastIndex * totalChrs, s.length()));
	}
}
